home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / demo / medowl.zip / SOURCE / MEDTSEAR.CPP < prev    next >
C/C++ Source or Header  |  1994-08-10  |  5KB  |  182 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   source\owl\editsear.cpp
  4. //   Implementation of class TEditWindow, an edit control that responds to
  5. //   Find, Replace and FindNext commands.
  6. //----------------------------------------------------------------------------
  7. #pragma hdrignore SECTION
  8. #include <owl\owlpch.h>
  9. #if defined(MAGMA)
  10. #include <owl\medtsear.h>
  11. #else
  12. #include <owl\editsear.h>
  13. #endif
  14. #include <owl\applicat.h>
  15. #include <owl\edit.h>
  16. #include <owl\findrepl.h>
  17. #include <cstring.h>
  18.  
  19. #if !defined(SECTION) || SECTION == 1
  20.  
  21. DEFINE_RESPONSE_TABLE1(TMagmaEditSearch, TMagmaEdit)
  22.   EV_COMMAND(CM_EDITFIND, CmEditFind),
  23.   EV_COMMAND(CM_EDITREPLACE, CmEditReplace),
  24.   EV_COMMAND(CM_EDITFINDNEXT, CmEditFindNext),
  25.   EV_REGISTERED(FINDMSGSTRING, EvFindMsg),
  26. END_RESPONSE_TABLE;
  27.  
  28. //
  29. // Construct a TMagmaEditSearch window given some initial text.
  30. //
  31. TMagmaEditSearch::TMagmaEditSearch(TWindow* parent,
  32.                          int             id,
  33.                          const char far* text,
  34.                          int x, int y, int w, int h,
  35.                          TModule*        module)
  36.   : TMagmaEdit(parent, id, text, x, y, w, h, 0, TRUE, module),
  37.     SearchData(FR_DOWN)
  38. {
  39.   Attr.Style |= ES_NOHIDESEL;
  40.   SearchDialog = 0;
  41.   SearchCmd = 0;
  42. }
  43.  
  44. TMagmaEditSearch::~TMagmaEditSearch()
  45. {
  46.   delete SearchDialog;
  47. }
  48.  
  49. //
  50. // Post a CM_EDITFIND or a CM_EDITREPLACE to re-open a previously open 
  51. // find or replace modeless dialog
  52. //
  53. void
  54. TMagmaEditSearch::SetupWindow()
  55. {
  56.   TMagmaEdit::SetupWindow();
  57.   if (SearchCmd)
  58.     PostMessage(WM_COMMAND, SearchCmd);
  59. }
  60.  
  61. //
  62. // Perform a search or replace operation based on information in SearchData
  63. //
  64. void
  65. TMagmaEditSearch::DoSearch()
  66. {
  67.   do {
  68.     if (Search(-1, SearchData.FindWhat, BOOL(SearchData.Flags&FR_MATCHCASE),
  69.                BOOL(SearchData.Flags&FR_WHOLEWORD),
  70.                !(SearchData.Flags&FR_DOWN)) >= 0) {
  71.       if (SearchData.Flags & (FR_REPLACE|FR_REPLACEALL))
  72.         Insert(SearchData.ReplaceWith);
  73.  
  74.     } else {
  75.       if (SearchData.Flags & (FR_FINDNEXT|FR_REPLACE)) {
  76.         string errTemplate(GetModule()->LoadString(IDS_CANNOTFIND));
  77.         char  errMsg[81];
  78.         wsprintf(errMsg, errTemplate.c_str(), (const char far*)SearchData.FindWhat);
  79.         TWindow* w = SearchDialog ? (TWindow*)SearchDialog : (TWindow*)this;
  80.         w->MessageBox(errMsg, GetApplication()->GetName(),
  81.                       MB_OK | MB_ICONEXCLAMATION);
  82.  
  83.       } else if (SearchData.Flags & FR_REPLACEALL)
  84.         break;
  85.     }
  86.   } while (SearchData.Flags & FR_REPLACEALL);
  87. }
  88.  
  89. //
  90. // Open the modeless Find commdlg
  91. //
  92. void
  93. TMagmaEditSearch::CmEditFind()
  94. {
  95.   if (!SearchCmd) {
  96.     SearchCmd = CM_EDITFIND;
  97.     delete SearchDialog;
  98.     SearchDialog = new TFindDialog(this, SearchData);
  99.     SearchDialog->Create();
  100.   }
  101. }
  102.  
  103. //
  104. // Open the modeless Replace commdlg
  105. //
  106. void
  107. TMagmaEditSearch::CmEditReplace()
  108. {
  109.   if (!SearchCmd) {
  110.     SearchCmd = CM_EDITREPLACE;
  111.     delete SearchDialog;
  112.     SearchDialog = new TReplaceDialog(this, SearchData);
  113.     SearchDialog->Create();
  114.   }
  115. }
  116.  
  117. //
  118. // Respond to the possible separate menu command to repeat the search
  119. //
  120. void
  121. TMagmaEditSearch::CmEditFindNext()
  122. {
  123.   if (SearchDialog)
  124.     SearchDialog->UpdateData();
  125.   SearchData.Flags |= FR_FINDNEXT;
  126.   DoSearch();
  127. }
  128.  
  129. //
  130. // Respond to the message sent by the modeless find/replace dialog by
  131. // performing a search. Or, if the dialog has terminated, zero search command
  132. //
  133. LRESULT
  134. TMagmaEditSearch::EvFindMsg(WPARAM, LPARAM lParam)
  135. {
  136.   PRECONDITION(SearchDialog);
  137.  
  138.   SearchDialog->UpdateData(lParam);
  139.   if (SearchData.Flags & FR_DIALOGTERM)
  140.     SearchCmd = 0;
  141.  
  142.   else
  143.     DoSearch();
  144.   return 0;
  145. }
  146.  
  147.  
  148. #endif
  149. #if !defined(SECTION) || SECTION == 2
  150.  
  151. IMPLEMENT_STREAMABLE1(TMagmaEditSearch, TMagmaEdit);
  152.  
  153. //
  154. // reads an instance of TMagmaEditSearch from the passed ipstream.
  155. // Re-opens the modeless find or replace dialog if one was up.
  156. //
  157. void*
  158. TMagmaEditSearch::Streamer::Read(ipstream& is, uint32 /*version*/) const
  159. {
  160.   ReadBaseObject((TMagmaEdit*)GetObject(), is);
  161.  
  162.   GetObject()->SearchData.Read(is);
  163.   is >> GetObject()->SearchCmd;
  164.   GetObject()->SearchDialog = 0;
  165.   return GetObject();
  166. }
  167.  
  168. //
  169. // writes the TMagmaEditSearch to the passed opstream
  170. //
  171. void
  172. TMagmaEditSearch::Streamer::Write(opstream& os) const
  173. {
  174.   WriteBaseObject((TMagmaEdit*)GetObject(), os);
  175.  
  176.   GetObject()->SearchData.Write(os);
  177.   os << GetObject()->SearchCmd;
  178. }
  179.  
  180. #endif
  181.  
  182.